package com.bojiu.lawsearch.lawsearch.service.impl;

import com.bojiu.lawsearch.lawsearch.mapper.AdminMapper;
import com.bojiu.lawsearch.lawsearch.mapper.GoodsSettingMapper;
import com.bojiu.lawsearch.lawsearch.mapper.SettingMapper;
import com.bojiu.lawsearch.lawsearch.model.*;
import com.bojiu.lawsearch.lawsearch.service.AdminService;
import com.bojiu.lawsearch.lawsearch.util.*;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.google.gson.JsonObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author: afdiao
 * @Date：2020/3/7 13:30
 * @Description:
 */
@Service
@Slf4j
public class AdminServiceImpl implements AdminService {

    @Autowired
    AdminMapper adminMapper;

    @Autowired
    SettingMapper settingMapper;

    @Autowired
    GoodsSettingMapper goodsSettingMapper;

    @Override
    public JsonResult login(String name, String password) {
        Map<String, Object> map = new HashMap<>();
        map.put("name", name);
        map.put("password", password);
        Object obj = adminMapper.selectOneByExample(UtilTools.createExampleByMap(map, Admin.class));
        if (null == obj) {
            return new JsonResult(ResultCode.ERRORPASSWORD);
        }
        Admin admin = (Admin) obj;
        //登录成功传token
        JsonObject jsonObject = new JsonObject();
        jsonObject.addProperty("role", admin.getRole());
        jsonObject.addProperty("userId", admin.getId());
        long time = new Date().getTime();
        //更新用户时间戳
        Admin update = new Admin();
        update.setLoginTime(String.valueOf(time));
        adminMapper.updateByExampleSelective(update, UtilTools.createExampleByKey("id", admin.getId(), Admin.class));

        //加密返回token
        jsonObject.addProperty("time", String.valueOf(time));
        try {
            String encrypt = EncryptUtil.encrypt(jsonObject.toString());
            return new JsonResult(ResultCode.SUCCESS, encrypt);
        } catch (Exception e) {
            log.error("error to login,error is :{}", e);
            return new JsonResult(ResultCode.FAIL);
        }
    }

    @Override
    public Page<Setting> getSettingList(int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize, true);
        List<Setting> settings = settingMapper.selectAll();
        PageInfo pageInfo = new PageInfo(settings);
        Page page = new Page(pageInfo.getPageNum(), pageInfo.getPageSize(), pageInfo.getPages(), pageInfo.getTotal(), settings);
        return page;
    }

    @Override
    public Page<GoodsSetting> getGoodsSettingList(int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize, true);
        List<GoodsSetting> goodsSettings = goodsSettingMapper.selectAll();
        PageInfo pageInfo = new PageInfo(goodsSettings);
        Page page = new Page(pageInfo.getPageNum(), pageInfo.getPageSize(), pageInfo.getPages(), pageInfo.getTotal(), goodsSettings);
        return page;
    }

    @Override
    public void updateValue(int id, String value) {
        Setting setting = new Setting();
        setting.setValue(value);
        settingMapper.updateByExampleSelective(setting, UtilTools.createExampleByKey("id", id, Setting.class));
    }

    @Override
    public void updateGoodsPrice(int id, int price) {
        WxGoods setting = new WxGoods();
        setting.setPrice(price);
        goodsSettingMapper.updateByExampleSelective(setting, UtilTools.createExampleByKey("id", id, WxGoods.class));
    }

    @Override
    public void createUser(Admin admin) {
        admin.setRole(Constances.USER);
        adminMapper.insertSelective(admin);
    }

    @Override
    public void deleteUser(int userId) {
        adminMapper.deleteByExample(UtilTools.createExampleByKey("id", userId, Admin.class));
    }

    @Override
    public List<Admin> getUserList() {
        return (List<Admin>) adminMapper.selectByExample(UtilTools.createExampleByKey("role", Constances.USER, Admin.class));
    }

    @Override
    public void updatePassword(int userId, String password) {
        Admin admin = new Admin();
        admin.setPassword(password);
        adminMapper.updateByExampleSelective(admin, UtilTools.createExampleByKey("id", userId, Admin.class));
    }
}
